<template>
	<view class="page-wrap">
		<view class="page-content2">
			<view class="swiper-box">
				<u-swiper :list="list1" @change="swiperChange" @click="swiperClick"></u-swiper>
			</view>
			<view class="page-main">
				<notice-box />
				<product-box />
				<video-box v-if="hasLeave" />
				<good-foundation ref="goodFoundation" />

			</view>
		</view>
		<navbar></navbar>
		
		<u-popup :show="showUpdate" mode="bottom" :closeOnClickOverlay="false"
			:safeAreaInsetBottom="false" bgColor="transparent">
			<view class="update flex-column">
				<view class="title">
					温馨提示
				</view>
				<view class="content">
					<text v-if="progress==0">检测到有新版本,请下载更新！</text>
					<text v-else>下载进度{{progress}}%,请勿关闭app！</text>
				</view>
				<view style="background-color: aliceblue; height: 1px;"></view>
				<view class="btn r-flex-row r-flex-items-center r-flex-center" @click="click_btn_update" v-if="progress==0">
					去下载
				</view>
			</view>
		</u-popup>

	</view>
</template>

<script>
	import base from '@/mixins/base.js'
	// import Navbar from '@/components/navbar/navbar.vue'
	import NoticeBox from '@/components/NoticeBox.vue'
	import ProductBox from './components/ProductBox.vue'
	import VideoBox from './components/VideoBox.vue'
	import GoodFoundation from './components/GoodFoundation.vue'
	import {
		getUserInfo,
		download,
		appConfig
	} from '@/config/api.js'
	import {
		VERSION
	} from '@/config/base.js'
	// export const iOS_VERSION = 100;
	// export const Android_VERSION = 100;
	export default {
		mixins: [base],
		components: {
			NoticeBox,
			ProductBox,
			VideoBox,
			GoodFoundation
		},
		data() {
			return {
				showUpdate: false,
				platform:null,
				progress:0,
				list1: [
					'https://static.hjzg.cc/static/banner/index1.jpg',
					'https://static.hjzg.cc/static/banner/index2.jpg',
					'https://static.hjzg.cc/static/banner/index3.jpg',
				],
				hasLeave: true,
				hasInvitationCode: false,
				appDownConfig:{}
			}
		},
		onLoad(info) {
			let invitationCode = info.invitationCode
			if (invitationCode) {
				this.hasInvitationCode = true
				let that = this
				uni.navigateTo({
					url: '/pages/login/login?invitationCode=' + invitationCode
				})
			}

		},
		onShow() {
			
			// #ifdef APP
			// this.judgeVersion()
			// #endif
			if (this.hasInvitationCode) {
				this.hasInvitationCode = false
				return
			}
			this.$store.commit('changeTab', 0)
			this.hasLeave = true
			this.$store.dispatch('getUserInfo')
			this.$nextTick(() => {
				this.$refs.goodFoundation.queryList()
			})
			this.platform = uni.getSystemInfoSync().platform;
			console.log(this.systemInfo)
			this.getAppConfig()

		},
		onHide() {
			this.hasLeave = false
		},
		methods: {
			async downTap() {
				const res = await download()
				// window.open(res)
				plus.runtime.openURL(res)
			},

			// async judgeVersion() {
			// 	console.log('本地版本号=', VERSION)
			// 	console.log('服务器版本号=',this.$store.state.userInfo.gender)
			// 	// if (VERSION !== this.$store.state.userInfo.gender) {
			// 	// 	this.showUpdate = true
			// 	// }
			// },
			swiperChange(item) {
				// console.log('swiperChang==', item)
			},
			swiperClick(item) {
				console.log('swiperChang==', item)
			},
			
			// 异步函数来处理数据
			getAppConfig () {
			     appConfig().then(response=>{
					 if(response){
					 	this.appDownConfig = response
					 	if (this.platform === 'android') {
					 	    console.log(plus.runtime.versionCode );
					 		plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
					 			if (response.apkVersion > wgtinfo.versionCode) {
					 				this.showUpdate = true
					 			}
					 		});
					 		
					 	} 
						// else if (platform === 'ios') {
						// 	if(this.appDownConfig.ipaUpdate){
						// 		plus.runtime.openURL(this.appDownConfig.ipaUrl)
						// 	}
					 // 	    // console.log('运行在iOS系统');
					 // 	}
					 }
				 }).catch(err=>{
					 
				 })
			    
			},
			// 点击立即更新后隐藏掉，然后播放背景音乐，检查apk地址是否有效
			click_btn_update() {
				// 如果apk安装包真实存在
				if (this.appDownConfig.apkUrl ) {
					uni.showToast({
						title: "正在下载安装包",
						icon: 'none'
					})
					//不显示立即更新按钮，显示进度条
					this.loading = true;
					let downloadTask = uni.downloadFile({
						url: this.appDownConfig.apkUrl ,
						success: (res) => {
							console.log(res)
							if (res.statusCode === 200) {
								uni.$u.toast('开始安装')
								//开始安装apk
								plus.runtime.install(res.tempFilePath, {
									force: true
								}, (res) => {
									console.log("安装成功", res);
									this.showUpdate = false
			
								}, (err) => {
									console.log("安装失败！", err);
									uni.showToast({
										title: "安装失败",
										icon: 'none'
									})
									this.progress = 0;
								});
							} else {
								console.log("下载安装包失败！");
								uni.showToast({
									title: "下载安装包失败",
									icon: 'none'
								})
							}
						},
			
					});
					//监听下载进度
					downloadTask.onProgressUpdate(res => {
						//返回百分比，比如1代表1%
						this.progress = res.progress;
						// uni.$u.toast('下载进度'+res.progress+"%")
					});
					//
				} else {
					console.log("apk安装包为空！");
					uni.showToast({
						title: "没有可用的安装包",
						icon: 'none'
					})
				}
			},
			
				
		}
	}
</script>

<style lang="scss" scoped>
	.update {
		// width: 100vw;
		background-color: white;
		border-radius: 10px;

		.btn {
			height: 44px;
			color: #2488FC;
			font-size: 17px;
			font-weight: 600;
		}

		.content {
			text-align: center;
			font-size: 15px;
			font-weight: 400;
			margin-top: 25px;
			margin: 25px 14px;
		}

		.title {
			text-align: center;
			font-size: 18px;
			font-weight: 600;
			margin-top: 15px;
		}
	}

	.page-wrap {
		position: relative;

		.img-a {
			width: 36px;
			height: 36px;
			position: absolute;
			left: 16px;
			top: 40px;
			z-index: 2;
		}

		.page-content2 {
			padding-bottom: 10px;
			background: url('@/static/img/home/bg.png') no-repeat;
			background-size: contain;
			background-color: #3294fd;

			.page-main {
				padding: 0 16px;
			}
		}
	}
</style>